package util;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.support.v4.content.FileProvider;
import android.widget.Toast;

import com.example.administrator.text.APICloud1Activity;
import com.example.administrator.text.APICloud2Activity;
import com.example.administrator.text.APICloud3Activity;
import com.example.administrator.text.APICloud4Activity;
import com.example.administrator.text.APICloudActivity;
import com.example.administrator.text.AccountListActivity;
import com.example.administrator.text.AppointTimeActivity;
import com.example.administrator.text.AuthorizationActivity;
import com.example.administrator.text.CaptureActivity;
import com.example.administrator.text.EntryPromptActivity;
import com.example.administrator.text.FaultFActivity;
import com.example.administrator.text.FeedbackActivity;
import com.example.administrator.text.HomeActivity;
import com.example.administrator.text.MapActivity;
import com.example.administrator.text.ModifyInformation1Activity;
import com.example.administrator.text.ModifyInformationActivity;
import com.example.administrator.text.PingActivity;
import com.example.administrator.text.ProblemActivity;
import com.example.administrator.text.ProblemItemActivity;
import com.example.administrator.text.RelieveBinDingActivity;
import com.example.administrator.text.ReportActivity;
import com.example.administrator.text.SetNewFiActivity;
import com.example.administrator.text.TimeRechargeActivity;
import com.example.administrator.text.VideoMonitorActivity;
import com.example.administrator.text.VoucherActivity;
import com.example.administrator.text.WeBinDingAccountActivity;
import com.example.administrator.text.WeTimeActivity;
import com.example.administrator.text.WeWiFiAccountActivity;
import com.example.administrator.text.WebViewActivity;
import com.example.administrator.text.WebshipActivity;
import com.uzmap.pkg.openapi.APICloud;

import org.json.JSONArray;

import java.io.File;

import app.MyApplication;
import data.SharedPreferencesUtils;
import util.serviceUtil.UpdataService;

import static util.HomeUtil.mTokenDB;

/**
 * Created by Administrator on 2016/5/5.
 * 页面跳转辅助类
 */
public class JumpActivityUtils {
    private static JumpActivityUtils mJumpActivityUtils;

    public JumpActivityUtils(){

    }

    public static JumpActivityUtils getInstance(){
        if(mJumpActivityUtils == null){
            synchronized (JumpActivityUtils.class){
                if(mJumpActivityUtils == null){
                    mJumpActivityUtils = new JumpActivityUtils();
                    return mJumpActivityUtils;
                }
            }
        }
        return mJumpActivityUtils;
    }

    /**
     * 跳转到提示登录页
     */
    public void jumEntrtPrompt(Context context) {

        Intent intent = new Intent(context, EntryPromptActivity.class);
        context.startActivity(intent);
        ((Activity) context).finish();
    }

    /**
     * 跳转到扫码显示页
     */
    public void jumAuthorization(Context context,String type,String sessionid) {

        Intent intent = new Intent(context, AuthorizationActivity.class);
        intent.putExtra("type",type);
        intent.putExtra("sessionid",sessionid);
        context.startActivity(intent);
    }

    /**
     * 跳转到可选列表页
     */
    public void jumAccountList(Context context) {

        Intent intent = new Intent(context, AccountListActivity.class);
        ((Activity) context).startActivityForResult(intent,WeBinDingAccountActivity.REQUSET);
    }

    /***
     * 跳转到首页
     */
    public void jumpHome(Context context, String home) {
        Intent intent = new Intent(context, HomeActivity.class);
        intent.putExtra("home", home);
        context.startActivity(intent);
    }

    /**
     * 跳转到ping页面
     */
    public void jumpPing(Context context){
        Intent intent = new Intent(context, PingActivity.class);
        context.startActivity(intent);
    }

    /**
     * 账号中心
     */
    public void jumpWeWiFiAccount(Context context, String account) {

        Intent intent = new Intent(context, WeWiFiAccountActivity.class);
        intent.putExtra("account", account);
        context.startActivity(intent);
    }

    /**
     * 修改个人信息
     */
    public void jumpModifypassword(Context context,String type) {
        Intent intent = new Intent(context,ModifyInformationActivity.class);
        intent.putExtra("type",type);
        ((Activity) context).startActivityForResult(intent,0);
    }

    /**
     * 修改个人信息1
     */
    public void jumpModifypassword1(Context context,String type) {
        Intent intent = new Intent(context, ModifyInformation1Activity.class);
        intent.putExtra("type",type);
        ((Activity) context).startActivityForResult(intent,0);
    }

    /***
     * 设置
     */
    public void jumpSetNetFi(Context context) {
        Intent intent = new Intent(context, SetNewFiActivity.class);
        context.startActivity(intent);
    }

    /***
     * 疑问
     */
    public void jumpSetNetFiItem(Context context) {
        Intent intent = new Intent(context, ProblemActivity.class);
        context.startActivity(intent);
    }

    /***
     * 疑问 item
     */
    public void jumpProblemItem(Context context,String type) {
        Intent intent = new Intent(context,ProblemItemActivity.class);
        intent.putExtra("type",type);
        context.startActivity(intent);
    }

    /***
     * 优惠券
     */
    public void jumpVoucher(Context context) {
        Intent intent = new Intent(context, VoucherActivity.class);
        context.startActivity(intent);
    }

    /***
     * 报障
     */
    public void jumpFaultF(Context context,String url) {
        Intent intent = new Intent(context, FaultFActivity.class);
        intent.putExtra("url",url);
        context.startActivity(intent);
    }

    /***
     * 绑定账号
     */
    public void jumpWeBinDingPhone(Context context, String string,String type) {

        Intent intent = new Intent(context,WeBinDingAccountActivity.class);
        intent.putExtra(WeBinDingAccountActivity.BINDING,string);
        intent.putExtra(WeBinDingAccountActivity.TYPR,type);
        context.startActivity(intent);
    }

    /***
     * 解除账号
     */
    public void jumpRelieveDing(Context context,String banding,String bindingid) {

        Intent intent = new Intent(context, RelieveBinDingActivity.class);
        intent.putExtra("binDing", banding);
        intent.putExtra("bindingid", bindingid);
        context.startActivity(intent);
    }

    /***
     * 意见反馈
     */
    public void jumpAbout(Context context) {

        Intent intent = new Intent(context, FeedbackActivity.class);
        context.startActivity(intent);
    }


    /***
     * 扫一扫
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void jumpZXing(Context context) {

        // 调用ZXIng开源项目源码  扫描二维码
        Intent intent = new Intent();
        intent.setClass(context, CaptureActivity.class);
        ((Activity) context).startActivityForResult(intent,1);
    }

    /***
     * 跳转 上网预约
     */
    public void jumpAppointTime(Context context,String type,String sessionid) {

        Intent intent = new Intent(context, AppointTimeActivity.class);
        intent.putExtra("type",type);
        intent.putExtra("sessionid",sessionid);
        ((Activity) context).startActivityForResult(intent,2);
    }

    /***
     * 跳转到WebView   +"&nasid=ethtrunk%2f3%3a3843.2437"
     */
    public void jumpWebView(Context context,String url,String title){
        if(title.equals("套餐") || title.equals("套餐购买") || title.equals("我的套餐") || title.equals("套餐充值")|| title.equals("会员购买")|| title.equals("会员")){
            int comboId = (int)SharedPreferencesUtils.getParam(context,"vip_comboId",1);
            String token = (String) SharedPreferencesUtils.getParam(context,mTokenDB,"");
            int good_tab = (int)SharedPreferencesUtils.getParam(context,"good_tab",1);
            url = url +"&comboid="+comboId+"&siteAreaid="+MyApplication.getApp().getSiteAreaid()+"&token="+token+"&good_tab="+good_tab;
        }
        Intent intent = new Intent(context,WebViewActivity.class);
        intent.putExtra("url",url);
        intent.putExtra("title",title);
        context.startActivity(intent);
    }

    /**
     * 安装app
     *
     */
    public void installApp(Context context, String filePath) {

       try{
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            // 判断是否是7.0
            if(Build.VERSION.SDK_INT >= 24){
                // 适配android7.0 ，不能直接访问原路径
                // 需要对intent 授权
                File file = new File(filePath);
                Uri apkUri = FileProvider.getUriForFile(context, "com.example.administrator.text.fileprovider", file);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.setDataAndType(apkUri,"application/vnd.android.package-archive");
            }
            else{
                intent.setDataAndType(Uri.fromFile(new File(filePath)),"application/vnd.android.package-archive");
            }
            context.startActivity(intent);
        }catch (Exception e){
           LogUtil.eE("",".......安装奔溃......."+e.getMessage());
            Toast.makeText(context,"下载成功，可以去文件夹中点击安装",Toast.LENGTH_SHORT).show();
        }
    }

    /***
     * 跳转到WebView
     */
    public void jumpWebshipoView(Context context, String url, String title) {

        Intent intent = new Intent(context, WebshipActivity.class);
        intent.putExtra("stringurl", url);
        intent.putExtra("stringtitle", title);
        context.startActivity(intent);
    }

    /**
     * 启动一个服务
     */
    public void jumpUpdataService(Context context, String url, String ver, String fileName) {

        Intent intent = new Intent(context, UpdataService.class);
        intent.putExtra("url",url);
        intent.putExtra("ver",ver);
        intent.putExtra("filepath", fileName);
        context.startService(intent);
    }

    /**
     * 跳转到视频监控页
     */
    public void jumpVideoMonitor(Context context) {
        Intent intent = new Intent(context, VideoMonitorActivity.class);
        context.startActivity(intent);
    }

    /**
     * 跳转到 时长充值
     */
    public void jumpTimeRecharge(Context context,String vip){
        Intent intent = new Intent(context, TimeRechargeActivity.class);
        intent.putExtra("vip",vip);
        context.startActivity(intent);
    }

    /**
     * 跳转到 我的时长
     */
    public void jumpWeTime(Context context){
        Intent intent = new Intent(context, WeTimeActivity.class);
        context.startActivity(intent);
    }

    /**
     * 跳转到 分享
     */
    public void jumpShare(Context context){
        Toast.makeText(context, "此功能暂没有开通，敬请期待", Toast.LENGTH_SHORT).show();
       /* Intent intent = new Intent(context, ShareActivity.class);
        context.startActivity(intent);*/
    }

    /**
     * 跳转到 地图
     */
    public void jumpMap(Context context,String type){
        Intent intent = new Intent(context, MapActivity.class);
        intent.putExtra("type",type);
        context.startActivity(intent);
    }

    /**
     * 跳转到测试页面
     */
    public void jumpVideo(Context context, String key,String val) {
        LogUtil.eE("",val);
        if(StringUtil.getStringUtilNew().isAppInstall(context,"com.sewise.demo.livevideo")){
            Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.sewise.demo.livevideo");
            intent.putExtra(key,val);
            context.startActivity(intent);
        }else {
            Toast.makeText(context,"请安装对应的app",Toast.LENGTH_SHORT).show();
        }

    }

    /**
     *客服
     */
    public void jumpReport(Context context){
        Intent intent = new Intent(context, ReportActivity.class);
        context.startActivity(intent);
    }

    /**
     * 启动APICloudActivity  白名单
     */
    public void jumpApiCloud(Context context,String id){
        LogUtil.eE("","...........jumpApiCloud.........."+id);
        Intent intent = new Intent(context, APICloudActivity.class);
        intent.putExtra("areaid",MyApplication.getApp().getSiteAreaid());
        intent.putExtra("ip","http://192.168.10.7:8080");
        intent.putExtra("userid",id);
        intent.putExtra("startUrl","file:///android_asset/widget/wgt/A6942476745795/html/page_manage.html");
        context.startActivity(intent);
    }

    /**
     * 启动APICloud1Activity 明细
     */
    public void jumpApiCloud1(Context context,String id,String token){
        LogUtil.eE("","...........jumpApiCloud.........."+id);
        Intent intent = new Intent(context,APICloud1Activity.class);
        intent.putExtra("token",token);
        intent.putExtra("userid",id);
        intent.putExtra("ip",MyApplication.getApp().getFWQString());
        intent.putExtra("startUrl","file:///android_asset/widget/wgt/A6942974923579/index.html");
        context.startActivity(intent);
    }

    /**
     * 启动APICloud2Activity 报障
     */
    public void jumpApiCloud2(Context context,String id){
        String nicKname = (String) SharedPreferencesUtils.getParam(context,ModifyInformationActivity.NICKNAME,"");
        String phone = (String) SharedPreferencesUtils.getParam(context, HomeUtil.mHUWePhone,"");
        String studentNumber = (String) SharedPreferencesUtils.getParam(context, HomeUtil.mSTUDENPhone, "");
        Intent intent = new Intent(context,APICloud2Activity.class);
        intent.putExtra("userid",id);
        intent.putExtra("nickname",nicKname);
        intent.putExtra("nickphone",phone);
        intent.putExtra("studentnumber",studentNumber);
        intent.putExtra("areaid",MyApplication.getApp().getSiteAreaid());
        if(MyApplication.getApp().getSiteAreaid() >=10000 || MyApplication.getApp().getSiteAreaid() == 0 ){
            intent.putExtra("server",Url.mATIAIRIP);
        }else {
            String fwq = MyApplication.getApp().getFWQString();
            String server = fwq.substring(0,fwq.indexOf("/p"));
            intent.putExtra("server",server);
            LogUtil.eE("","...server.."+server);
        }
        //intent.putExtra("startUrl","file:///android_asset/widget/wgt/A6055971850547/index.html");
        intent.putExtra("startUrl","file:///android_asset/widget/wgt/A6057122872380/index.html");
        context.startActivity(intent);
    }

    /**
     * 启动APICloud3Activity 消息
     */
    public void jumpApiCloud3(Context context,JSONArray appJsonData,String id){
        LogUtil.eE("","...........jumpApiCloud..........");
        Intent intent = new Intent(context,APICloud3Activity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("userid",id);
        intent.putExtra("pushjson",appJsonData.toString());
        intent.putExtra("startUrl","file:///android_asset/widget/wgt/A6057890942489/html/newsList.html");
        context.startActivity(intent);
    }

    /**
     * 启动APICloud4Activity 福利
     * bottomView 首页导航的个数
     * clickState 点击的状态 0代表未点击
     * type == 1 福利  type == 2 地图
     */
    public void jumpApiCloud4(Context context,int topHeight,int bottomView,int type,String id,String Token){
        LogUtil.eE("","...........jumpApiCloud..........");
        Intent intent = new Intent(context,APICloud4Activity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        intent.putExtra("topHeight",topHeight-3);
        intent.putExtra("bottomHeight",49);
        intent.putExtra("bottomView",bottomView);
        intent.putExtra("siteAreaid",MyApplication.getApp().getSiteAreaid());
        intent.putExtra("pattern",MyApplication.getApp().getmPattern());
        intent.putExtra("userId",id);
        intent.putExtra("userToken",Token);
        intent.putExtra("appFWQ",MyApplication.getApp().getFWQString());
        intent.putExtra("apMac",getInformation.getNewGetInformation(context).getWiFiMac());
        if(type == 1){
            intent.putExtra("type",1);
        }else if(type == 2){
            intent.putExtra("type",2);
        }
        intent.putExtra("startUrl","file:///android_asset/widget/wgt/A6050234198860/html/welfareMain.html");
        context.startActivity(intent);
        ((Activity)context).overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }

    /**
     * 启动APICloud3Activity 设备列表
     * type  类型  0设备   1扫码
     */
    public void jumpApiCloud5(Context context,int type,String id,String token,String sessionid){
        LogUtil.eE("","...........jumpApiCloud..........");
        int pattern = MyApplication.getApp().getmPattern();
        int vipid = (int) SharedPreferencesUtils.getParam(context, HomeUtil.mVIPCombonum,1);
        Intent intent = new Intent(context,APICloud2Activity.class);
        intent.putExtra("siteAreaid",MyApplication.getApp().getSiteAreaid());
        intent.putExtra("pattern",pattern);
        intent.putExtra("userId",id);
        intent.putExtra("userToken",token);
        intent.putExtra("vipId",vipid);
        intent.putExtra("ip",getInformation.getNewGetInformation(context).getI_IP());
        intent.putExtra("mac",getInformation.getNewGetInformation(context).getIMAC());
        intent.putExtra("apMac",getInformation.getNewGetInformation(context).getWiFiMac());
        intent.putExtra("appFWQ",Url.mSSIDSERVER);
        switch (type){
            case 0:
                int deviceCount = (int) SharedPreferencesUtils.getParam(context,HomeUtil.mPackage_deviceCount,1);
                int comboId = (int)SharedPreferencesUtils.getParam(context,"vip_comboId",1);
                int good_tab = (int)SharedPreferencesUtils.getParam(context,"good_tab",1);
                int pledge = (int) SharedPreferencesUtils.getParam(context, HomeUtil.mPledge,1);
                intent.putExtra("deviceCount",deviceCount);
                intent.putExtra("good_tab",good_tab);
                intent.putExtra("comboId",comboId);
                intent.putExtra("pledge",pledge);
                intent.putExtra("startUrl","file:///android_asset/widget/wgt/A6065671119420/html/index.html");
                context.startActivity(intent);
                break;
            case 1:
                intent.putExtra("sessionid",sessionid);
                intent.putExtra("startUrl","file:///android_asset/widget/wgt/A6065671119420/html/qrcode_analysis.html");
                context.startActivity(intent);
                break;
        }
    }
}
